import asyncio
import playwright.async_api
from playwright.async_api import async_playwright
from item.up_info import UPInfo
import pipeline as pl
import os
import sys
from settings import ORIGIN_UP_HOME_PAGE_LINK, ORIGIN_PERSONAL_VIDEO_LINK
from settings import ORIGIN_DATA_PATH, OUTPUT_DATA_PATH
from settings import OUTPUT_UP_HOMEPAGE_DETAIL
import openpyxl
import re
from openpyxl import Workbook

print(os.getcwd())

def prepare():
    wb = Workbook()
    ws = wb.active
    print("-------prepare-------")
    ws['A1'] = 'UID'
    ws['B1'] = 'Follow'
    ws['C1'] = 'Fans'
    ws['D1'] = 'Liked'
    ws['E1'] = 'Played'
    ws['F1'] = 'ChargeNum'
    ws['G1'] = 'VideoNum'

    try:
        wb.save(OUTPUT_DATA_PATH + OUTPUT_UP_HOMEPAGE_DETAIL)
    except Exception as e:
        print(e)
        wb.close()
    else:
        wb.close()

def load_file():
        wb = openpyxl.load_workbook(ORIGIN_DATA_PATH + ORIGIN_UP_HOME_PAGE_LINK)
        ws = wb.active
        up_homepage_link = []
        for line in ws:
            link = str(line[0].value)
            up_homepage_link.append(link)
        return up_homepage_link

async def get_charge_num(page:playwright.async_api.Page):
    try:
        wait = await page.locator('span.elec-count').text_content(timeout=1000)
    except playwright.async_api.TimeoutError as e:
        return 0
    else:
        return re.findall('\d+', wait)[0]

async def extract_up_info(context: playwright.async_api.BrowserContext, link):
    if not link:
        return
    page = await context.new_page()
    await page.route("**/*", lambda route: route.abort()
    if route.request.resource_type == "image"
    else route.continue_())
    await page.goto(link, wait_until="networkidle")
    print(link)
    p_uid = await page.locator('span.info-value').all_text_contents()
    p_follows = await page.locator('p#n-gz').all_text_contents()
    p_fans = await page.locator('p#n-fs').all_text_contents()
    uid = p_uid[0]
    follows = re.findall("\d+",p_follows[0])[0]
    fans = re.findall("\d+",p_fans[0])[0]
    liked = 0
    played = 0
    p_create_video_num = await page.locator('.video .section-title .count').all_text_contents()
    create_video_num = p_create_video_num[0]
    charged_num = await get_charge_num(page)
    item = UPInfo(uid, follows, fans, liked, played, charged_num, create_video_num)
    await page.close()
    return item

async def fetch(context):
    urls = load_file()
    page = await context.new_page()
    prepare()
    try:
        # following: `document`, `stylesheet`, `image`, `media`, `font`, `script`, `texttrack`, `xhr`, `fetch`,
        # `eventsource`, `websocket`, `manifest`, `other`.
        await page.route("**/*", lambda route: route.abort()
        if route.request.resource_type == "image"
        else route.continue_())
        for l in urls:
            item = await extract_up_info(context, l)
            pl.add_up_item(OUTPUT_UP_HOMEPAGE_DETAIL, item)


    except playwright.async_api.TimeoutError as e:
        print(e)
    else:
        print('over')
        await page.close()

async def bilibili() -> None:
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context()
        await fetch(context=context)
        await context.close()
        await browser.close()

def main(argv):
    loop = asyncio.get_event_loop()
    loop.run_until_complete(bilibili())


if __name__ == "__main__":
    main(sys.argv)

